﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SofireCodeGenerator.UI
{
    public partial class SettingForm : Form
    {
        public SettingForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            this.ShowInTaskbar = false;

            txt_EntitySuffix.Text = GlobalSetting.EntitySuffix;
            txt_XSqlDirectory.Text = GlobalSetting.XSqlDirectory;
            txt_XSqlDirectory.Enabled = GlobalSetting.EnabledXSql;
            txt_TemplatesDirectory.Text = GlobalSetting.TemplatesDirectory;
            cb_AutoAliasName.Checked = GlobalSetting.AutoAliasName;
            cb_AlwaysNullable.Checked = GlobalSetting.AlwaysNullable;
            cb_EnabledXSql.Checked = GlobalSetting.EnabledXSql;

            folderBrowserDialog1.SelectedPath = GlobalSetting.TemplatesDirectory;

            this.LoadTemplates();

            txt_XSqlDirectory.TextChanged += GlobalSetting_Changed;
            txt_EntitySuffix.TextChanged += GlobalSetting_Changed;
            txt_TemplatesDirectory.TextChanged += GlobalSetting_Changed;
            cb_AutoAliasName.CheckedChanged += GlobalSetting_Changed;
            cb_AlwaysNullable.CheckedChanged += GlobalSetting_Changed;
            cb_EnabledXSql.CheckedChanged += GlobalSetting_Changed;
        }

        private bool GlobalSettingChanged;

        void GlobalSetting_Changed(object sender, EventArgs e)
        {
            GlobalSettingChanged = true;
        }

        private void btn_SaveAndClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if(GlobalSettingChanged)
            {
                GlobalSetting.EntitySuffix = txt_EntitySuffix.Text;
                GlobalSetting.XSqlDirectory = txt_XSqlDirectory.Text;
                GlobalSetting.TemplatesDirectory = txt_TemplatesDirectory.Text;
                GlobalSetting.AutoAliasName = cb_AutoAliasName.Checked;
                GlobalSetting.AlwaysNullable = cb_AlwaysNullable.Checked;
                GlobalSetting.EnabledXSql = cb_EnabledXSql.Checked;
                GlobalSetting.Save();
            }
        }

        private void txt_TemplatesDirectory_DoubleClick(object sender, EventArgs e)
        {
            if(folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txt_TemplatesDirectory.Text = folderBrowserDialog1.SelectedPath;
                this.LoadTemplates();
            }
        }
        private void LoadTemplates()
        {
            if(!Directory.Exists(txt_TemplatesDirectory.Text))
            {
                this.ShowError("目录不存在！");
                txt_TemplatesDirectory_DoubleClick(null, null);
                return;
            }
            lbox_Templates.Items.Clear();
            foreach(var item in Directory.GetFiles(txt_TemplatesDirectory.Text))
            {
                lbox_Templates.Items.Add(Path.GetFileName(item));
            }
        }

        private void lbox_Templates_DoubleClick(object sender, EventArgs e)
        {
            var filename = lbox_Templates.SelectedItem.ToString();
            var fullPath = Path.Combine(txt_TemplatesDirectory.Text, filename);
            var f = new PreviewForm(Path.GetFileNameWithoutExtension(filename), File.ReadAllText(fullPath, Encoding.UTF8));
            f.HideTab2();
            f.ShowDialog();
            if(f.IsChanged && this.ShowYesNo("内容已修改，是否保存 " + filename + " ？"))
            {
                File.WriteAllText(fullPath, f.EditorText, Encoding.UTF8);
            }
        }

        private void cb_BuildXSql_CheckedChanged(object sender, EventArgs e)
        {
            txt_XSqlDirectory.Enabled = cb_EnabledXSql.Checked;
        }
    }
}
